package com.hainatong.shiphelper.module.workermgr.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import androidx.databinding.DataBindingUtil;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.ItemDepartmentViewBinding;
import com.hainatong.shiphelper.databinding.ItemOrgLevelViewBinding;
import com.hainatong.shiphelper.module.warehouse.entity.OrgUser;
import com.hainatong.shiphelper.module.warehouse.entity.ShipOrgBean;

import java.util.List;

public class OrgExpandableAdapter extends BaseExpandableListAdapter
{

    private Context mContext;

    private List<ShipOrgBean> shipOrgBeans;


    public OrgExpandableAdapter(Context mContext)
    {
        this.mContext = mContext;
    }


    public OrgExpandableAdapter(Context mContext, List<ShipOrgBean> shipOrgBeans)
    {
        this.mContext = mContext;
        this.shipOrgBeans = shipOrgBeans;
    }

    public List<ShipOrgBean> getShipOrgBeans()
    {
        return shipOrgBeans;
    }

    public void setShipOrgBeans(List<ShipOrgBean> shipOrgBeans)
    {
        this.shipOrgBeans = shipOrgBeans;
        this.notifyDataSetChanged();
    }

    @Override
    public int getGroupCount()
    {
        //避免出现数据空 导致奔溃的问题;
        return shipOrgBeans!=null?shipOrgBeans.size():0;
    }

    @Override
    public int getChildrenCount(int groupPosition)
    {
        return (shipOrgBeans!=null && shipOrgBeans.get(groupPosition).getChildren()!=null)?shipOrgBeans.get(groupPosition).getChildren().size():0;
    }

    @Override
    public Object getGroup(int groupPosition)
    {
        return shipOrgBeans.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition)
    {
        return shipOrgBeans.get(groupPosition).getChildren().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition)
    {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition)
    {
        return childPosition;
    }

    @Override
    public boolean hasStableIds()
    {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {
        ShipOrgBean shipOrg = (ShipOrgBean) getGroup(groupPosition);
        //View view = LayoutInflater.from(mContext).inflate(R.layout.item_department_view,null);
        ItemDepartmentViewBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext),R.layout.item_department_view, null,false);

        if(isExpanded)
        {
            binding.ivArrowTag.setImageResource( R.mipmap.ic_arrow_down );
        }
        else
        {
            binding.ivArrowTag.setImageResource( R.mipmap.ic_arrow_right );
        }
        int count = 0;
        if( shipOrg.getChildren() != null && !shipOrg.getChildren().isEmpty() )
        {
            count = shipOrg.getChildren().size();
        }
        binding.tvWorkerCount.setText( String.valueOf( count ) );
        binding.setDepartment( shipOrg );
        binding.executePendingBindings();
        return binding.getRoot();
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
    {
        OrgUser orgUser = (OrgUser) getChild(groupPosition,childPosition);
        //View view = LayoutInflater.from(mContext).inflate(R.layout.item_department_view,null);
        ItemOrgLevelViewBinding levelBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext),R.layout.item_org_level_view, null,false);
        levelBinding.tvDepartName.setText( orgUser.getRoleName() );

        return levelBinding.getRoot();
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition)
    {
        return true;
    }


}
